/*     */ package com.yc.upload;
/*     */ 
/*     */ import btsm.bean.sysPro;
/*     */ import com.yc.dao.FileDAO;
/*     */ import com.yc.dao.model.FileBO;
/*     */ import com.yc.service.FolderService;
/*     */ import com.yc.util.UtilBtsm;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.SocketException;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Date;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import java.util.StringTokenizer;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.apache.commons.fileupload.FileItem;
/*     */ import org.apache.commons.fileupload.FileUploadException;
/*     */ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
/*     */ import org.apache.commons.fileupload.servlet.ServletFileUpload;
/*     */ import org.apache.commons.logging.Log;
/*     */ import org.apache.commons.logging.LogFactory;
/*     */ 
/*     */ public class UploadServlet extends HttpServlet
/*     */ {
/*  37 */   private static final Log log = LogFactory.getLog(UploadServlet.class);
/*  38 */   private String tempPath = "C:\\upload\\tmp";
/*  39 */   private String uploadPath = "";
/*  40 */   private File tempPathFile = null;
/*  41 */   private FileDAO fileDao = null;
/*  42 */   private String propertiesFileName = "/btsm.properties";
/*  43 */   private double uploadSize = 0.0D;
/*  44 */   private static String importFilePath = null;
/*     */ 
/*     */   public void init()
/*     */   {
/*  48 */     this.fileDao = new FileDAO();
/*     */     try
/*     */     {
/*  51 */       Properties p = new Properties();
/*  52 */       InputStream in = FolderService.class.getResourceAsStream(this.propertiesFileName);
/*  53 */       p.load(in);
/*  54 */       in.close();
/*  55 */       if (p.containsKey("uploadSize"))
/*     */       {
/*  57 */         String strSize = p.getProperty("uploadSize");
/*  58 */         strSize = strSize.substring(0, strSize.indexOf('M'));
/*  59 */         this.uploadSize = (Double.parseDouble("1024") * Double.parseDouble(strSize));
/*     */       }
/*  61 */       sysPro sPro = new sysPro();
/*  62 */       String strUploadPath = sPro.getUploadpath();
/*  63 */       if ((strUploadPath != null) && (!"".equalsIgnoreCase(strUploadPath)))
/*     */       {
/*  65 */         this.uploadPath = strUploadPath.replaceAll("//", "\\");
/*     */       }
/*  68 */       else if (p.containsKey("uploadPath")) {
/*  69 */         this.uploadPath = p.getProperty("uploadPath");
/*     */       }
/*     */ 
/*  73 */       if (p.containsKey("importFilePath")) {
/*  74 */         importFilePath = p.getProperty("importFilePath");
/*     */       }
/*  76 */       log.info("importFilePath=" + importFilePath);
/*  77 */       log.info(sPro.getUploadpath());
/*  78 */       log.info(this.uploadPath);
/*     */     }
/*     */     catch (Exception e) {
/*  81 */       e.printStackTrace();
/*     */     }
/*     */   }
/*     */ 
/*     */   public static String getImportUploadFilePath() {
/*  86 */     return importFilePath;
/*     */   }
/*     */ 
/*     */   private void createTempFolder(String strFilePath)
/*     */   {
/*  91 */     this.tempPathFile = new File(this.tempPath);
/*  92 */     boolean bFile = this.tempPathFile.exists();
/*     */ 
/*  94 */     if (bFile) {
/*  95 */       log.info("The folder exists.");
/*     */     } else {
/*  97 */       log.info("The folder do not exist,now trying to create a one...");
/*  98 */       bFile = this.tempPathFile.mkdirs();
/*  99 */       if (bFile)
/* 100 */         log.info("Create successfully!");
/*     */       else
/* 102 */         log.info("Disable to make the folder,please check the disk is full or not.");
/*     */     }
/*     */   }
/*     */ 
/*     */   private String getFilepPathForDatabase(String strFolderID)
/*     */   {
/* 109 */     String strFilePath = this.fileDao.getFolderPath(strFolderID);
/* 110 */     StringTokenizer token = new StringTokenizer(strFilePath, ",");
/* 111 */     StringBuffer filePathBuffer = new StringBuffer();
/* 112 */     while (token.hasMoreTokens())
/*     */     {
/* 114 */       filePathBuffer.append(token.nextToken());
/* 115 */       filePathBuffer.append("\\\\");
/*     */     }
/* 117 */     return filePathBuffer.toString();
/*     */   }
/*     */ 
/*     */   private Map getShareFolderPath()
/*     */   {
/* 123 */     Map map = new HashMap();
/* 124 */     StringBuffer buff = new StringBuffer();
/* 125 */     StringBuffer buffData = new StringBuffer();
/* 126 */     buff.append(this.uploadPath);
/* 127 */     StringTokenizer token = new StringTokenizer(this.uploadPath, "\\");
/* 128 */     while (token.hasMoreTokens())
/*     */     {
/* 130 */       buffData.append(token.nextToken());
/* 131 */       buffData.append("\\\\");
/*     */     }
/* 133 */     buffData.append("share");
/* 134 */     buff.append("\\");
/* 135 */     buff.append("share");
/* 136 */     map.put("DATA", buffData.toString());
/* 137 */     map.put("PATH", buff.toString());
/* 138 */     return map;
/*     */   }
/*     */ 
/*     */   private String getFilepPath(String strFolderID)
/*     */   {
/* 144 */     if ("root".endsWith(strFolderID))
/*     */     {
/* 146 */       Map map = getShareFolderPath();
/* 147 */       UtilBtsm.createFolder(map.get("PATH").toString());
/* 148 */       return map.get("DATA").toString();
/*     */     }
/*     */ 
/* 151 */     return this.fileDao.getFolderPath(strFolderID);
/*     */   }
/*     */ 
/*     */   private void saveFile(String strFileName, String strFolderID, String strUserID, String strFilePath, long size)
/*     */   {
/* 157 */     StringTokenizer token = new StringTokenizer(strFilePath, "\\");
/* 158 */     StringBuffer filePathBuffer = new StringBuffer();
/*     */ 
/* 160 */     int count = token.countTokens();
/* 161 */     int i = 0;
/* 162 */     while (token.hasMoreTokens())
/*     */     {
/* 164 */       filePathBuffer.append(token.nextToken());
/* 165 */       if (i < count - 1)
/* 166 */         filePathBuffer.append("\\\\");
/* 167 */       i++;
/*     */     }
/*     */ 
/* 170 */     FileBO fileBo = new FileBO();
/* 171 */     fileBo.setFileId(this.fileDao.queryMaxFileID());
/* 172 */     fileBo.setFileName(strFileName);
/* 173 */     fileBo.setFolderId(strFolderID);
/* 174 */     fileBo.setUserId(strUserID);
/* 175 */     fileBo.setFilePath(filePathBuffer.toString());
/* 176 */     fileBo.setUploadTime(new Date());
/* 177 */     fileBo.setFileSize(size / 1024L);
/* 178 */     if ("root".equalsIgnoreCase(strFolderID))
/*     */     {
/* 180 */       fileBo.setIsShare("true");
/* 181 */       fileBo.setShareBeginTime(new Date());
/*     */     }
/* 183 */     this.fileDao.saveFile(fileBo);
/*     */   }
/*     */ 
/*     */   private boolean isOverSize(double fileSize, String strUserID)
/*     */   {
/* 194 */     boolean isOk = true;
/* 195 */     List fileSizeList = null;
/*     */     try {
/* 197 */       fileSizeList = this.fileDao.getAllFileSize(strUserID);
/*     */     } catch (SQLException e) {
/* 199 */       e.printStackTrace();
/* 200 */       return false;
/*     */     }
/* 202 */     double uploadFileSize = 0.0D;
/* 203 */     if ((fileSizeList != null) && (fileSizeList.size() > 0))
/*     */     {
/* 205 */       for (int i = 0; i < fileSizeList.size(); i++)
/*     */       {
/* 207 */         uploadFileSize += Double.parseDouble(fileSizeList.get(i).toString());
/*     */       }
/*     */     }
/* 210 */     log.info("fileSize      =[" + fileSize + "]");
/* 211 */     log.info("uploadFileSize=[" + uploadFileSize + "]");
/* 212 */     log.info("uploadSize    =[" + this.uploadSize + "]");
/* 213 */     if (fileSize + uploadFileSize >= this.uploadSize)
/* 214 */       isOk = false;
/* 215 */     return isOk;
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 222 */     log.info("开始上传文件");
/* 223 */     String strReturn = "{success:true,msg:'文件上传成功！'}";
/*     */     try
/*     */     {
/* 226 */       DiskFileItemFactory factory = new DiskFileItemFactory();
/* 227 */       ServletFileUpload sevletFileUpload = new ServletFileUpload(factory);
/*     */ 
/* 231 */       createTempFolder(this.tempPath);
/* 232 */       factory.setRepository(this.tempPathFile);
/*     */ 
/* 234 */       sevletFileUpload.setHeaderEncoding("utf-8");
/* 235 */       List fileItems = sevletFileUpload.parseRequest(request);
/* 236 */       Iterator iter = fileItems.iterator();
/* 237 */       String parentFolderID = null;
/* 238 */       String parentFolderName = null;
/* 239 */       String fileName = null;
/*     */ 
/* 241 */       while (iter.hasNext())
/*     */       {
/* 243 */         FileItem item = (FileItem)iter.next();
/* 244 */         String name = item.getName();
/* 245 */         if (item.isFormField()) {
/* 246 */           name = item.getFieldName();
/* 247 */           if ("strFolderParentID".equals(name))
/* 248 */             parentFolderID = new String(item.getString().getBytes("iso8859-1"), "utf-8");
/* 249 */           if ("strFolderParentName".equals(name))
/* 250 */             parentFolderName = new String(item.getString().getBytes("iso8859-1"), "utf-8");
/* 251 */           String value = new String(item.getString().getBytes("iso8859-1"), "utf-8");
/* 252 */           log.info(name + ":" + value);
/*     */         }
/*     */       }
/* 255 */       log.info("*******[" + parentFolderID + ":" + parentFolderName + "]*****");
/* 256 */       String strFolderPath = getFilepPath(parentFolderID);
/* 257 */       UtilBtsm.createFolder(strFolderPath);
/* 258 */       log.info("strFolderPath : " + strFolderPath);
/*     */ 
/* 260 */       iter = fileItems.iterator();
/* 261 */       while (iter.hasNext())
/*     */       {
/* 263 */         FileItem item = (FileItem)iter.next();
/* 264 */         fileName = item.getName();
/* 265 */         if (!item.isFormField())
/*     */         {
/* 267 */           String strUserID = request.getSession().getAttribute("stxtUName").toString();
/* 268 */           double fileSize = item.getSize() / 1024L;
/* 269 */           boolean isOver = isOverSize(fileSize, strUserID);
/* 270 */           if ("root".equalsIgnoreCase(parentFolderID))
/* 271 */             isOver = true;
/* 272 */           if (!isOver)
/*     */           {
/* 274 */             response.setContentType("text/html; charset=utf-8");
/* 275 */             strReturn = "{success:false,msg:'文件上传失败,每个用户总共只能上传【" + this.uploadSize / 1024.0D + "M】的文件，请删除其他文件后再上传该文件！'}";
/* 276 */             response.getWriter().print(strReturn);
/* 277 */             return;
/*     */           }
/*     */ 
/* 280 */           log.info("上传文件名是：[" + item + "]");
/* 281 */           if (fileName.lastIndexOf("\\") != -1)
/* 282 */             fileName = fileName.substring(fileName.lastIndexOf("\\"));
/* 283 */           File fullFile = new File(fileName);
/* 284 */           File savedFile = new File(strFolderPath, fullFile.getName());
/* 285 */           item.write(savedFile);
/*     */ 
/* 288 */           saveFile(fileName, parentFolderID, strUserID, savedFile.getAbsolutePath(), savedFile.length());
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/*     */     }
/*     */     catch (FileUploadException eup)
/*     */     {
/* 296 */       eup.printStackTrace();
/* 297 */       log.error(eup);
/*     */     }
/*     */     catch (SocketException clietEx) {
/* 300 */       log.warn("客户取消下载");
/*     */     }
/*     */     catch (Exception e) {
/* 303 */       strReturn = "{success:false,msg:'文件上传失败！'}";
/* 304 */       e.printStackTrace();
/*     */     }
/* 306 */     log.info(strReturn);
/* 307 */     response.setContentType("text/html; charset=utf-8");
/* 308 */     response.getWriter().print(strReturn);
/*     */   }
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 314 */     doPost(request, response);
/*     */   }
/*     */ }

/* Location:           E:\java\apache-tomcat-6.0.36\webapps\btsm\WEB-INF\classes\
 * Qualified Name:     com.yc.upload.UploadServlet
 * JD-Core Version:    0.6.2
 */